package com.library.management.controller;

import com.library.management.common.PageRequest;
import com.library.management.common.PageResult;
import com.library.management.common.Result;
import com.library.management.entity.Book;
import com.library.management.entity.BookCategory;
import com.library.management.service.BookCategoryService;
import com.library.management.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书控制器
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private BookCategoryService bookCategoryService;

    /**
     * 图书列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<BookCategory> categories = bookCategoryService.listAll();
        model.addAttribute("categories", categories);
        return "book/list";
    }

    /**
     * 图书详情页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "book/detail";
    }

    /**
     * 图书添加页面
     */
    @GetMapping("/add")
    public String add(Model model) {
        List<BookCategory> categories = bookCategoryService.listAll();
        model.addAttribute("categories", categories);
        return "book/add";
    }

    /**
     * 图书编辑页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Book book = bookService.getById(id);
        List<BookCategory> categories = bookCategoryService.listAll();
        model.addAttribute("book", book);
        model.addAttribute("categories", categories);
        return "book/edit";
    }

    /**
     * 查询图书列表
     */
    @GetMapping("/api/list")
    @ResponseBody
    public Result<PageResult<Book>> list(Book book, PageRequest pageRequest) {
        PageResult<Book> pageResult = bookService.list(book, pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询图书
     */
    @GetMapping("/api/get/{id}")
    @ResponseBody
    public Result<Book> get(@PathVariable Long id) {
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    /**
     * 添加图书
     */
    @PostMapping("/api/add")
    @ResponseBody
    public Result<Integer> add(@RequestBody Book book) {
        int result = bookService.add(book);
        return Result.success(result);
    }

    /**
     * 更新图书
     */
    @PutMapping("/api/update")
    @ResponseBody
    public Result<Integer> update(@RequestBody Book book) {
        int result = bookService.update(book);
        return Result.success(result);
    }

    /**
     * 删除图书
     */
    @DeleteMapping("/api/delete/{id}")
    @ResponseBody
    public Result<Integer> delete(@PathVariable Long id) {
        int result = bookService.delete(id);
        return Result.success(result);
    }

    /**
     * 更新图书库存
     */
    @PutMapping("/api/updateStock")
    @ResponseBody
    public Result<Integer> updateStock(@RequestParam Long id, @RequestParam Integer stock) {
        int result = bookService.updateStock(id, stock);
        return Result.success(result);
    }
}